﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

public partial class lottery_manage : System.Web.UI.Page
{
    protected DataTable result = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log.aspx", true);
        }
        if (!IsPostBack)
        {
            result.Columns.Add("color");
            result.Columns.Add("l_title");
            result.Columns.Add("l_id");
            result.Columns.Add("l_image");
            result.Columns.Add("l_fprize");
            result.Columns.Add("l_fnum");
            result.Columns.Add("l_sprize");
            result.Columns.Add("l_snum");
            result.Columns.Add("l_tprize");
            result.Columns.Add("l_tnum");
            result.Columns.Add("l_sume");
            result.Columns.Add("l_rules");
            result.Columns.Add("l_start");
            result.Columns.Add("l_end");
            string name = Session["USERNAME"].ToString();
            SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc1.Open();

            SqlCommand get_discount = new SqlCommand();
            get_discount.Connection = sc1;
            get_discount.CommandText = "select LOTTERY_ID,LOTTERY_TITLE,LOTTERY_IMAGE,FPRIZE_NUM,SPRIZE_NUM,TPRIZE_NUM,SUM_EXPECT,RULES,LOTTERY_START,LOTTERY_END,a.PRODUCT_NAME as 'FPRIZE',b.PRODUCT_NAME as 'SPRIZE',c.PRODUCT_NAME as 'TPRIZE' from INFO_PRODUCT as a,INFO_PRODUCT as b,INFO_PRODUCT as c,INFO_LOTTERY where INFO_LOTTERY.ENTER_ID=@eid1 and INFO_LOTTERY.FPRIZE_ID=a.PRODUCT_ID and INFO_LOTTERY.SPRIZE_ID=b.PRODUCT_ID and INFO_LOTTERY.TPRIZE_ID=c.PRODUCT_ID";
            get_discount.Parameters.AddWithValue("@eid1",Session["ENTERID"].ToString());
            SqlDataReader r = get_discount.ExecuteReader();
            DateTime start;
            DateTime end;
            DateTime now = DateTime.Now;
            while (r.Read())
            {
                DataRow row = result.NewRow();
                row["l_title"] = r.GetString(r.GetOrdinal("LOTTERY_TITLE"));
                row["l_id"] = r.GetString(r.GetOrdinal("LOTTERY_ID"));
                row["l_image"] = r.GetString(r.GetOrdinal("LOTTERY_IMAGE"));
                row["l_fprize"] = r.GetString(r.GetOrdinal("FPRIZE"));
                row["l_sprize"] = r.GetString(r.GetOrdinal("SPRIZE"));
                row["l_tprize"] =  r.GetString(r.GetOrdinal("TPRIZE"));
                row["l_fnum"] = r.GetInt32(r.GetOrdinal("FPRIZE_NUM"));
                row["l_snum"] = r.GetInt32(r.GetOrdinal("SPRIZE_NUM"));
                row["l_tnum"] = r.GetInt32(r.GetOrdinal("TPRIZE_NUM"));
                row["l_sume"] = r.GetInt32(r.GetOrdinal("SUM_EXPECT"));
                row["l_rules"] = r.GetString(r.GetOrdinal("RULES"));
               
                start = r.GetDateTime(r.GetOrdinal("LOTTERY_START"));
                end = r.GetDateTime(r.GetOrdinal("LOTTERY_END"));
                row["l_start"] = start.ToString();
                row["l_end"] = end.ToString();
                if (now > start && now < end)
                {
                    row["color"] = "Red";
                }
                else row["color"] = "Black";
                result.Rows.Add(row);
            }
            DataBind();
            sc1.Close();
        }
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (Session["IDENTITY"].Equals("employee") == true)
        {
            Response.Redirect("error_auth.aspx", true);
        }
        if (e.CommandName == "undercarriage")
        {
            string id = e.CommandArgument.ToString();
            SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc1.Open();

            try
            {
                SqlCommand get_image = new SqlCommand("select LOTTERY_IMAGE from INFO_LOTTERY where LOTTERY_ID=@iddd", sc1);
                get_image.Parameters.AddWithValue("@iddd", id);
                string imagepath = get_image.ExecuteScalar().ToString();
                imagepath = Server.MapPath(imagepath);
                SqlCommand get_name = new SqlCommand("select LOTTERY_TITLE from INFO_LOTTERY where LOTTERY_ID=@id2", sc1);
                get_name.Parameters.AddWithValue("@id2", id);
                string d_name = get_name.ExecuteScalar().ToString();

                if (File.Exists(imagepath))
                    File.Delete(imagepath);
                //删除DISCOUNT_ATTEND中的记录并记录到ATTEND_RECORD当中
                label3.Text = "开始迁移您的数据至备份数据库中...";
                #region 迁移参与记录到历史记录表中
                string[] indid = new string[1000];//预留1000条记录的容量
                DateTime[] adate = new DateTime[1000];
                SqlCommand get_indid = new SqlCommand("select IND_ID,ATTEND_DATE from LOTTERY_ATTEND where LOTTERY_ID=@id3", sc1);
                get_indid.Parameters.AddWithValue("@id3", id);
                SqlDataReader s = get_indid.ExecuteReader();
                int tt = 0;
                while (s.Read())
                {
                    indid[tt] = s.GetString(s.GetOrdinal("IND_ID"));
                    adate[tt] = s.GetDateTime(s.GetOrdinal("ATTEND_DATE"));
                    tt++;
                }
                s.Close();
                //将获取的记录一一插入ATTENDRECORD
                for (int p = 0; p < tt; p++)
                {
                    SqlCommand insert_record = new SqlCommand("insert into ATTEND_RECORD values (@id4,@etype,@ename,@adate)", sc1);
                    insert_record.Parameters.AddWithValue("@id4", indid[p]);
                    insert_record.Parameters.AddWithValue("@etype", "LOTTERY");
                    insert_record.Parameters.AddWithValue("@ename", d_name);
                    insert_record.Parameters.AddWithValue("@adate", adate[p]);
                    while (Application["sqllock"] == "lock")
                    {//检测是否已被锁住
                        System.Threading.Thread.Sleep(2000);
                    }
                    Application["sqllock"] = "lock";//上锁          
                    try
                    {
                        insert_record.ExecuteNonQuery();
                    }
                    catch (Exception ex_sql)
                    {
                        Application["sqllock"] = "unlock";//解锁
                        Page.RegisterStartupScript("ss", "<script>alert('" + ex_sql.StackTrace + ex_sql.Message + "')</script>");
                        return;
                    }
                    finally
                    {
                        Application["sqllock"] = "unlock";//解锁
                    }
                }
                //将DISCOUNT_ATTEND中记录删除
                SqlCommand del_at = new SqlCommand("delete from LOTTERY_ATTEND where LOTTERY_ID=@id5", sc1);
                del_at.Parameters.AddWithValue("@id5", id);
                while (Application["sqllock"] == "lock")
                {//检测是否已被锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁          
                try
                {
                    del_at.ExecuteNonQuery();
                }
                finally
                {
                    Application["sqllock"] = "unlock";//解锁
                }


                #endregion
                label3.Text = "数据迁移完毕.";
                SqlCommand del_dis = new SqlCommand("delete from INFO_LOTTERY where LOTTERY_ID=@id", sc1);
                del_dis.Parameters.AddWithValue("@id", id);
                while (Application["sqllock"] == "lock")
                {//检测是否已被锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁          
                try
                {
                    del_dis.ExecuteNonQuery();
                }
                finally
                {

                    Application["sqllock"] = "unlock";//解锁
                }
                Page.RegisterStartupScript("ss", "<script>alert('下架成功！')</script>");
                return;
            }
            catch (Exception ex_sql)
            {
                Page.RegisterStartupScript("ss", "<script>alert('"+ex_sql.Message+ex_sql.StackTrace+"！')</script>");
                return;
            }
           


        }
    }
}